package com.example.pictureManage.service;

import com.example.pictureManage.FileNode;
import com.example.pictureManage.service.mainViewService.imageBoxService;
import com.example.pictureManage.util.FileUtil;
import javafx.scene.control.Label;
import javafx.scene.layout.TilePane;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDateTime;

/***
 * 用于保存主窗口的多个状态
 *
 */
public class mainViewStage {
    //服务层
    private static final imageBoxService imageServer = new imageBoxService();

    //选中的文件目录
    private static FileNode fileNode = null;

    //显示图片的   父组件/主组件
    private static TilePane tilePane = null;

    private static Label fileDetailLabel = null;

    public static Label getFileDetailLabel() {
        return fileDetailLabel;
    }

    public static FileNode getFileNode() {
        return fileNode;
    }

    public static TilePane getTilePane() {
        return tilePane;
    }

    /**
     * 用于填充右侧的预览图片
     * @param selectedItem  选中的目录
     * @param contentPane  应该填充的元素
     * @param fileDetail   详细标签
     *                     这三个参数都要保留一份
     */
    public static void displaySelectedFolder(FileNode selectedItem, TilePane contentPane, Label fileDetail) {
        //清空右侧的预览图片
        contentPane.getChildren().clear();
        //获取到选中的路径
        Path selectedPath = selectedItem.getFilePath();
        if (Files.isDirectory(selectedPath) && Files.isReadable((selectedPath))) {
            try {
//                System.out.println(LocalDateTime.now());
                Files.list(selectedPath)
                        .filter(subPath -> !Files.isDirectory(subPath)) // 仅处理文件
                        .filter(FileUtil::isImageFile) // 检查是否为图片
                        .forEach(subPath -> {
                            //这里就利用到了imageServer层
                            imageServer.createImageItem(subPath,contentPane);
                        });
                fileNode = selectedItem;
                tilePane = contentPane;
                fileDetailLabel = fileDetail;
//                System.out.println(LocalDateTime.now());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 重新加载
     */
    public static void reload(){
        if(fileNode != null && tilePane != null && fileDetailLabel != null){
            displaySelectedFolder(fileNode,tilePane, fileDetailLabel);
        }

    }
}
